/*
 * <one line to give the program's name and a brief idea of what it does.>
 * Copyright (C) 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "shippart.hpp"
#include "datatable.hpp"

ShipPart::ShipPart(const upgradeType type, uint16_t level) : m_Type(type) {
	m_Level = level;
	updateValue();
}

void ShipPart::updateValue() {
	switch(m_Type) {
		case upgradeType::HULL:
			m_Value = DataTable::upgradeStats[m_Level][2];
			break;

		case upgradeType::REACTOR:
			m_Value = DataTable::upgradeStats[m_Level][1];
			break;

		case upgradeType::WEAPON:
			m_Value = DataTable::upgradeStats[m_Level][0];
			break;

		case upgradeType::WINGS:
			m_Value = DataTable::upgradeStats[m_Level][3];
			break;
	}
}

uint16_t ShipPart::upgradeMaxLevel(const uint16_t level, const resource & res) {
	uint32_t count = 0;
	resource price;

	// TODO rewrite ?
	for(uint32_t i = level + 1; ; ++i) {
		price += upgradePrice(i);

		if(res >= price) {
			count++;
		} else {
			break;
		}
	}

	return count;
}

resource ShipPart::upgradePrice(uint16_t level) {
	return DataTable::upgradePrice[level];
}

resource ShipPart::upgradePriceDiff(uint16_t begin, uint16_t end) {
	resource result;

	if(begin < end) {
		result = DataTable::upgradePriceTotal[end];
		result -= DataTable::upgradePriceTotal[begin];
	}

	return result;
}
